﻿Option Strict On

Imports System.Net
Imports Sunts.HyperV.Common
Imports System.Net.Mail


Class frmMailNotificationSetup
    Private Sub EmailNotificationSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            FillDataGrid()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Using frmMailRecipient As New frmMailRecipient
            frmMailRecipient.ShowDialog()
        End Using
        FillDataGrid()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        Dim MailRecipient As MailRecipient

        Try
            MailRecipient = CType(dgvMailRecipients.SelectedRows(0).DataBoundItem, MailRecipient)

            Using frmMailRecipient As New frmMailRecipient
                frmMailRecipient.MailRecipient = MailRecipient
                frmMailRecipient.ShowDialog()
            End Using
            FillDataGrid()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
   

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim MailRecipient As MailRecipient
        Try
            MailRecipient = CType(dgvMailRecipients.SelectedRows(0).DataBoundItem, MailRecipient)
            MailRecipient.Delete(My.Settings.ConnectionString)
            FillDataGrid()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub FillDataGrid()
        dgvMailRecipients.DataSource = MailRecipient.SelectAll(My.Settings.ConnectionString)
        If dgvMailRecipients.Rows.Count > 0 Then
            dgvMailRecipients.Columns("MailRecipientID").Visible = False
            dgvMailRecipients.Columns("EmailAddress").HeaderText = "E-mail Address"
        End If
    End Sub


    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        If bgwSendMail.IsBusy = False Then
            bgwSendMail.RunWorkerAsync()
        End If
    End Sub

    Private Sub bgwSendMail_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwSendMail.DoWork
        If bgwSendMail.CancellationPending = True Then
            e.Cancel = True
        Else
            Dim MailServer As MailServer
            Dim MailRecipientCollection As New MailRecipientCollection
            Dim MailRecipient As MailRecipient = CType(dgvMailRecipients.SelectedRows(0).DataBoundItem, Sunts.HyperV.Common.MailRecipient)

            MailServer = MailServer.SelectAll(My.Settings.ConnectionString).Item(0)

            SendTestMail(MailServer, MailRecipient)
        End If
    End Sub

    Sub SendTestMail(ByVal MailServer As MailServer, ByVal MailRecipient As MailRecipient)

        Dim netcred As New NetworkCredential(MailServer.SMTPUsername, MailServer.SMTPPassword)

        Try
            Using SmtpClient As New SmtpClient(MailServer.MailServer, MailServer.Port)
                Using MailMessage As New MailMessage
                    MailMessage.To.Add(New MailAddress(MailRecipient.EmailAddress))
                    MailMessage.From = New MailAddress(MailServer.SenderEmailAddress, MailServer.SenderName)
                    MailMessage.Subject = "Hyper-V Backup Test Message"
                    MailMessage.Body = "This is a test message from Hyper-V backup, if you are reading this then the mail configuration is complete"
                    MailMessage.IsBodyHtml = True

                    If MailServer.SMTPAuthentication = True Then SmtpClient.Credentials = netcred

                    SmtpClient.Send(MailMessage)
                End Using
            End Using

            MessageBox.Show("The mail has been successfully submitted, please check your inbox for a test mail")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

End Class